<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spring 事务失效原因深度解析</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            position: relative;
            overflow: hidden;
        }
        
        .hero-gradient::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23ffffff' fill-opacity='0.05'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
        }
        
        .content-card {
            background: white;
            border-radius: 16px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        pre {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 8px;
            padding: 20px;
            overflow-x: auto;
            position: relative;
            margin: 20px 0;
        }
        
        pre::before {
            content: 'Java';
            position: absolute;
            top: 10px;
            right: 10px;
            background: rgba(255, 255, 255, 0.1);
            padding: 4px 12px;
            border-radius: 4px;
            font-size: 12px;
            color: #888;
        }
        
        code {
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
            background: #f0f0f0;
            padding: 2px 6px;
            border-radius: 4px;
            color: #e83e8c;
            font-size: 14px;
        }
        
        pre code {
            background: none;
            padding: 0;
            color: #d4d4d4;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 20px;
            border-radius: 12px;
            margin: 30px 0;
            position: relative;
            overflow: hidden;
        }
        
        .highlight-box::before {
            content: '\f0eb';
            font-family: 'Font Awesome 6 Free';
            font-weight: 900;
            position: absolute;
            right: 20px;
            top: 50%;
            transform: translateY(-50%);
            font-size: 48px;
            opacity: 0.2;
        }
        
        .mermaid {
            margin: 30px 0;
            text-align: center;
        }
        
        .icon-box {
            width: 60px;
            height: 60px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 12px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 24px;
            margin-bottom: 20px;
            box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
        }
        
        .list-item {
            position: relative;
            padding-left: 30px;
            margin-bottom: 15px;
        }
        
        .list-item::before {
            content: '\f00c';
            font-family: 'Font Awesome 6 Free';
            font-weight: 900;
            position: absolute;
            left: 0;
            top: 3px;
            color: #667eea;
        }
        
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .animate-fadeInUp {
            animation: fadeInUp 0.6s ease-out;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 relative">
        <div class="container mx-auto px-6 relative z-10">
            <div class="max-w-4xl mx-auto text-center animate-fadeInUp">
                <h1 class="text-5xl md:text-6xl font-bold mb-6" style="font-family: 'Noto Serif SC', serif;">
                    Spring 事务失效原因深度解析
                </h1>
                <p class="text-xl md:text-2xl opacity-90 leading-relaxed">
                    掌握事务管理的核心要点，避免常见陷阱，构建稳定可靠的企业级应用
                </p>
                <div class="mt-10 flex justify-center space-x-4">
                    <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                        <i class="fas fa-clock mr-2"></i>阅读时间：8分钟
                    </span>
                    <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                        <i class="fas fa-tag mr-2"></i>Spring Framework
                    </span>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <!-- Introduction Card -->
        <div class="content-card p-8 mb-8 animate-fadeInUp">
            <div class="highlight-box">
                <p class="text-lg leading-relaxed">
                    Spring 事务失效的原因通常与配置、注解使用、事务边界、异常处理、数据源问题以及 AOP 代理有关。通过检查这些方面，可以解决大多数事务失效的问题，确保事务管理的正确性。
                </p>
            </div>
        </div>

        <!-- Visualization -->
        <div class="content-card p-8 mb-8 animate-fadeInUp">
            <h2 class="text-2xl font-bold mb-6 text-gray-800">事务失效原因关系图</h2>
            <div class="mermaid">
                graph TD
                    A[Spring事务失效] --> B[配置问题]
                    A --> C[注解使用不当]
                    A --> D[事务边界问题]
                    A --> E[异常处理不当]
                    A --> F[数据源问题]
                    A --> G[AOP代理问题]
                    
                    B --> B1[未启用事务管理]
                    B --> B2[事务管理器未配置]
                    
                    C --> C1[注解未应用或应用错误]
                    C --> C2[传播行为配置不当]
                    C --> C3[隔离级别不适合]
                    
                    D --> D1[事务跨越多个方法]
                    D --> D2[非Spring管理代码中失效]
                    
                    E --> E1[未捕获的异常]
                    E --> E2[Checked异常未配置回滚]
                    
                    F --> F1[数据库连接问题]
                    F --> F2[数据库不支持事务]
                    
                    G --> G1[事务代理未生效]
                    G --> G2[AOP配置错误]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:3px,color:#fff
                    style B fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                    style C fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                    style D fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                    style E fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                    style F fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                    style G fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Section 1 -->
        <div class="content-card p-8 mb-8 animate-fadeInUp">
            <div class="icon-box">
                <i class="fas fa-cog"></i>
            </div>
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">1. 事务管理配置问题</h2>
            
            <div class="mb-8">
                <h3 class="text-xl font-semibold mb-4 text-gray-700">
                    <i class="fas fa-exclamation-circle text-red-500 mr-2"></i>未启用事务管理
                </h3>
                <p class="text-gray-600 mb-4 leading-relaxed">
                    确保在 Spring 配置中启用了事务管理。这通常通过添加 <code>@EnableTransactionManagement</code> 注解或者在 XML 配置中设置事务管理器来实现。
                </p>
                <pre><code>@Configuration
@EnableTransactionManagement
public class AppConfig {
    // Configuration code
}</code></pre>
            </div>

            <div>
                <h3 class="text-xl font-semibold mb-4 text-gray-700">
                    <i class="fas fa-database text-blue-500 mr-2"></i>事务管理器未配置
                </h3>
                <p class="text-gray-600 mb-4 leading-relaxed">
                    需要确保在 Spring 配置中定义了事务管理器 Bean。例如，使用 <code>@Bean</code> 注解或 XML 配置定义一个 <code>DataSourceTransactionManager</code> 或 <code>JpaTransactionManager</code>。
                </p>
                <pre><code>@Bean
public PlatformTransactionManager transactionManager(DataSource dataSource) {
    return new DataSourceTransactionManager(dataSource);
}</code></pre>
            </div>
        </div>

        <!-- Section 2 -->
        <div class="content-card p-8 mb-8 animate-fadeInUp">
            <div class="icon-box">
                <i class="fas fa-code"></i>
            </div>
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">2. 事务注解使用不当</h2>
            
            <div class="space-y-6">
                <div>
                    <h3 class="text-xl font-semibold mb-4 text-gray-700">
                        <i class="fas fa-tag text-purple-500 mr-2"></i>@Transactional 注解未应用或应用错误
                    </h3>
                    <p class="text-gray-600 mb-4 leading-relaxed">
                        确保在需要事务管理的方法上正确使用 <code>@Transactional</code> 注解。注意，<code>@Transactional</code> 只能作用于 public 方法，对于 private、protected 方法无效。
                    </p>
                    <pre><code>@Service
public class MyService {
    @Transactional
    public void performTransaction() {
        // Method code
    }
}</code></pre>
                </div>

                <div>
                    <h3 class="text-xl font-semibold mb-4 text-gray-700">
                        <i class="fas fa-share-alt text-green-500 mr-2"></i>事务传播行为配置不当
                    </h3>
                    <p class="text-gray-600 mb-4 leading-relaxed">
                        事务传播行为（如 <code>REQUIRED</code>, <code>REQUIRES_NEW</code>, <code>NESTED</code>）配置不正确可能导致事务失效。需要根据业务逻辑选择合适的传播行为。
                    </p>
                    <pre><code>@Transactional(propagation = Propagation.REQUIRES_NEW)
public void performNestedTransaction() {
    // Method code
}</code></pre>
                </div>

                